package com.bdth.weget.api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bdth.weget.api.service.JwtService;
import com.bdth.weget.common.CommonList;
import com.bdth.weget.common.CommonResult;
import com.bdth.weget.api.entity.User;
import com.bdth.weget.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2019/7/25.
 */
@RestController()
@RequestMapping("/api")
public class UserController {

    private static final String JWT_HEADER_NAME = "x-user-id";

    @Autowired
    private UserService userService;

    @Autowired
    private JwtService jwtService;

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public CommonResult<User> login(HttpServletResponse response, @RequestParam String username, @RequestParam String password){
        User bean = userService.doLogin(username,password);
        String jwt = jwtService.generateToken(String.valueOf(bean.getId()));
        response.setHeader(JWT_HEADER_NAME, jwt);
        return CommonResult.success(bean);
    }

    @RequestMapping(value = "userList",method = RequestMethod.GET)
    public CommonList<User> listUser(){
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        List<User> list = userService.selectList(entityWrapper);
        return CommonList.success(list);
    }
}
